package com.pb.controller;

import com.pb.anno.Log;
import com.pb.pojo.Dept;
import com.pb.pojo.Result;
import com.pb.service.DeptService;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.logging.Logger;

@Slf4j
@RequestMapping("/depts")
@RestController
public class DeptController {

    @Autowired
    private DeptService deptService;

    //private static final Logger log = (Logger) LoggerFactory.getLogger(DeptController.class);
    //@RequestMapping(value = "/depts",method = RequestMethod.GET)
    @GetMapping
    public Result list() {
        //System.out.println("查询全部部门数据");
        log.info("查询全部部门数据");
        List<Dept> deptList = deptService.findAll();
        return Result.success(deptList);
    }
//    @DeleteMapping("/depts")
//    public Result delete(HttpServletRequest request){
//        String idStr = request.getParameter("id");
//        int id = Integer.parseInt(idStr);
//        System.out.println("根据id删除部门数据："+id);
//        return Result.success();
//    }
//    @DeleteMapping("/depts")
//    public Result delete(@RequestParam(value = "id", required = false) Integer id){
//        System.out.println("根据id删除部门数据："+id);
//        return Result.success();
//    }
    // 名字相同直接传参名即可
    @Log
    @DeleteMapping
    public Result delete(Integer id){
        //System.out.println("根据id删除部门数据："+id);
        log.info("根据id删除部门数据："+id);
        deptService.deleteById(id);
        return Result.success();
    }
    @Log
    @PostMapping
    public Result add(@RequestBody Dept dept){
        //System.out.println("新增部门数据："+dept);
        log.info("新增部门数据："+dept);
        deptService.add(dept);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result getInfo(@PathVariable Integer id){
        //System.out.println("根据id查询部门数据："+id);
        log.info("根据id查询部门数据：{}"+id);
        Dept dept = deptService.getById(id);
        return Result.success(dept);
    }
    @Log
    @PutMapping
    public Result update(@RequestBody Dept dept){
        //System.out.println("修改部门数据："+dept);
        log.info("修改部门数据：{}"+dept);
        deptService.update(dept);
        return Result.success();
    }

}
